package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.UserAttr;
import util.Helper;

import dao.UserDao;

public class Login extends ServletTemplate{
   
    /**
     * 一个默认的编号 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 执行POST操作 
     */
    public void doPost(HttpServletRequest request,
                         HttpServletResponse response) throws IOException, ServletException{
        super.doPost(request, response);
        
        String userName     = ((String)this.getRequestParam("user_name")).trim();
        String userPasswd   = ((String)this.getRequestParam("user_passwd")).trim();
        if(userName.equals("") || userPasswd.equals("")) {
            this.setRequestAttr("errorInfo", "用户及密码都不能为空！");
            this.forward("hint.jsp");
        }
        UserDao userDao     = new UserDao();
        UserAttr userAttr   = userDao.userLogin(userName, Helper.getMD5Str(userPasswd));
        if(null == userAttr) {
            this.setRequestAttr("errorInfo", "用户名或用户密码不正确！");
            this.forward("hint.jsp");
            return;
        }
        this.setLoginInfo(userAttr);
        this.redirect("./polllist");
    }
   
    /**
     * 设置用户的登陆信息 
     */
    private void setLoginInfo(UserAttr userAttr) {
        this.setSessionAttr("userAttr", userAttr);
        //timestamp是毫秒级的
        this.setSessionAttr("validTime", Helper.getTimeStamp() + 3600 * 1000);
    }
    
}
